package com.shyroke.controller;

import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.text.DecimalFormat;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpSession;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.shyroke.calculation.AttributeWeightCal;
import com.shyroke.calculation.ExpertWeightCal;
import com.shyroke.calculation.GroupDecisionInfoCal;
import com.shyroke.calculation.QiCal;
import com.shyroke.calculation.SiCal;
import com.shyroke.calculation.TopSisCal;
import com.shyroke.util.ConvertUtil;

import entity.ConvertExpert;
import entity.Expert;
import entity.GroupDesicionDto;

@Controller
@RequestMapping(value="analyzeAction")
public class AnalyzeAction {

	
	@RequestMapping(value="getAnalyze")
	public ModelAndView getAnalyze(ModelAndView model,HttpSession session) throws NoSuchMethodException, SecurityException, IllegalAccessException, IllegalArgumentException, InvocationTargetException, IOException {
		Map<Integer,Expert> expertMap = ExpertAction.expertMap;
		if(expertMap == null || expertMap.size() == 0) {
			throw new RuntimeException("专家的中智集不能为空");
		}
		List<ConvertExpert> convertExpertList = ConvertUtil.covert(expertMap);
		
		//  获取4个专家的重要性
		Map<String,Double> expertImportance = ExpertWeightCal.getExpertWeight();
		
		// 获取属性权重
		Map<String,Double> attributeWeight = AttributeWeightCal.calAttributeWeight(convertExpertList,expertImportance);
	
		//群体决策信息,List.size代表决策方案数量
		List<GroupDesicionDto> desicionDtos = GroupDecisionInfoCal.getGroupDesion(expertMap,expertImportance);
		
		//计算群体效用 值Si <x1,0.402>表示决策方案x1的效用值为Si
		Map<String,Double> siMap = new SiCal().getSi(attributeWeight, desicionDtos);
		
		//计算个体遗憾 值Ri <x1,0.402>表示决策方案x1的个体遗憾值为Ri
		Map<String,Double> RiMap = new SiCal().getRi(attributeWeight, desicionDtos);
		
		//计算折中评价值Qi<x1,0.77> 表示决策方案x1的评价值为0.77
		Map<String,Double> qiMap =	new QiCal(siMap, RiMap).getQi();
		
		/**
		 * x轴配置
		 */
		Map<String,Double> xCongfig = new HashMap<String, Double>();
		xCongfig.put("x1", 0.1);
		xCongfig.put("x2", 0.2);
		xCongfig.put("x3", 0.3);
		xCongfig.put("x4", 0.4);
		xCongfig.put("x5", 0.5);
		xCongfig.put("x6", 0.6);
		xCongfig.put("x7", 0.7);
		xCongfig.put("x8", 0.8);
		xCongfig.put("x9", 0.9);
		xCongfig.put("x10", 0.10);
		
		/**
		 * 折线决策方案x1/x2/x3的配置
		 */
		Map<String,Double> x1Config = new HashMap<String, Double>();
		Map<String,Double> x2Config = new HashMap<String, Double>();
		Map<String,Double> x3Config = new HashMap<String, Double>();
		
		QiCal qiCal = new QiCal(siMap, RiMap);
		
		for(double i=0.1;i<=1;i=i+0.1) {
			qiCal.NANTA = i;
			Map<String,Double> qiMap1 = qiCal.getQi();
			
			for(Map.Entry<String,Double> entry : qiMap1.entrySet()) {
				if(entry.getKey().equals("x1")) {
					x1Config.put("x"+Math.round(Double.valueOf(new DecimalFormat("#.0").format(i))*10), entry.getValue());
				}else if(entry.getKey().equals("x2")) {
					x2Config.put("x"+Math.round(Double.valueOf(new DecimalFormat("#.0").format(i))*10), entry.getValue());
				}else if(entry.getKey().equals("x3")) {
					x3Config.put("x"+Math.round(Double.valueOf(new DecimalFormat("#.0").format(i))*10), entry.getValue());
				}
			}
		}
		
		TopSisCal topSisCal = new TopSisCal();
		
		//key代表第几个方案，value代表该方案对应的D+
		Map<String,Double> D_plust = topSisCal.getDPlus(attributeWeight,desicionDtos,"D+");
		
		Map<String,Double> D_jian = topSisCal.getDPlus(attributeWeight,desicionDtos,"D-");
		
		Map<String,Double> C_X = topSisCal.getC(D_plust,D_jian);
		
		String result = topSisCal.getResult(C_X);
		
		model.addObject("xCongfig", xCongfig);
		model.addObject("x1Config", x1Config);
		model.addObject("x2Config", x2Config);
		model.addObject("x3Config", x3Config);
		model.addObject("result", (String)session.getAttribute("result"));
		model.setViewName("analyze");
	
		return model;
	}
}
